//
//  Cache.m
//  Custom
//
//  Created by dllo on 15/11/26.
//  Copyright © 2015年 KPY. All rights reserved.
//

#import "Cache.h"

@implementation Cache



+ (void)saveToCachPath:(NSString *)cachePath netWorkData:(id)responseObject; {
    
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
    NSString *cacheFolderPath = [sandBoxPath stringByAppendingString:[NSString stringWithFormat:@"/Caches/%@", cachePath]];
    NSString *cacheFilePath = [cacheFolderPath stringByAppendingString:@"/Cache.plist"];
    NSLog(@"%@", NSHomeDirectory());
    
    // 将数据缓存 归档
    NSFileManager *manager = [NSFileManager defaultManager];
    [manager createDirectoryAtPath:cacheFolderPath withIntermediateDirectories:YES attributes:nil error:nil];
    [NSKeyedArchiver archiveRootObject:responseObject toFile:cacheFilePath];
    
//    NSDictionary *cacheDic = [NSKeyedUnarchiver unarchiveObjectWithFile:cacheFilePath];
    
}

+ (NSDictionary *)selectAll:(NSString *)cachePath {
    
    // 路径
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
    NSString *cacheFolderPath = [sandBoxPath stringByAppendingString:[NSString stringWithFormat:@"/Caches/%@", cachePath]];
    NSString *cacheFilePath = [cacheFolderPath stringByAppendingString:@"/Cache.plist"];
    NSLog(@"%@", NSHomeDirectory());
    
    // 判断网络状况
    // 如果没有网，判断缓存数据是否为空
    //
    NSDictionary *cacheDic = [NSKeyedUnarchiver unarchiveObjectWithFile:cacheFilePath];
    
//    NSLog(@"%@", cacheDic);
    
    return cacheDic;
    
//        if (!(cacheDic == nil)) {
//          NSArray *saveArr = responseObject;
//            return saveArr;
//        }
//        else{
//            NSLog(@"没有数据");
//            return nil;
//        }
    
}



@end
